<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div id="app">
      <div>
        <span>全选:</span>
        <input type="checkbox" v-model="xz" @click="txt" />
        <button @click="txt">反选</button>
        <ul>
          <li v-for="(inex ,id) in arr">
            <input type="checkbox" v-model="inex.checked" />
            <span>{{inex.name}}</span>
          </li>
        </ul>
      </div>
    </div>
    <script src="../vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        //定义属性
        data: {
          arr: [
            {
              name: "猪八戒",
              checked: false,
            },
            {
              name: "孙悟空",
              checked: false,
            },
            {
              name: "唐僧",
              checked: false,
            },
            {
              name: "白龙马",
              checked: false,
            },
          ],
          xz: false,
        },
        //定义方法
        methods: {
          txt() {
            this.xz = !this.xz;
            this.arr.forEach((inex, id) => {
              inex.checked = this.xz;
            });
          },
        },
        watch: {
          arr: {
            deep: true,
            immediate: true,
            handler() {
              this.xz = this.arr.every((inex) => inex.checked === true);
            },
          },
        },
      });
    </script>
  </body>
</html>
